package com.Bigbluebook.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.Bigbluebook.Activity.MainActivity;

import com.Bigbluebook.R;
import com.Bigbluebook.Service.PostApiService;
import com.Bigbluebook.entity.Post;
import com.Bigbluebook.entity.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostFragment extends Fragment {

    private EditText contentEditText;
    private EditText tagEditText;
    private int userId;
    private Button publishButton;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_post, container, false);

        contentEditText = view.findViewById(R.id.pub_news_content);
        tagEditText = view.findViewById(R.id.pub_news_tag);
        publishButton = view.findViewById(R.id.pub_news_btn);
        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout);


        publishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = contentEditText.getText().toString();
                String tag = tagEditText.getText().toString();

                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user_info", Context.MODE_PRIVATE);
                userId = sharedPreferences.getInt("userid", -1);

                publish(content, tag,userId);
            }
        });

        return view;
    }

    private void setupSwipeRefreshLayout() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 在这里执行刷新操作
                refreshContent();
            }
        });
    }

    private void refreshContent() {
        // 刷新内容的逻辑
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 2000); // 延迟2秒关闭刷新动画
    }
    public void publish(String content, String tag,int UserId) {
        User user = new User();
        user.setId(UserId);
        Post post=new Post(user,content,tag);

        if (content.isEmpty()) {
            Toast.makeText(getContext(), "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (tag.isEmpty()) {
            Toast.makeText(getContext(), "标签不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        PostApiService postService = new PostApiService();

        postService.createPost(post,new Callback<Post>() {

            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(getContext(), "发布成功", Toast.LENGTH_SHORT).show();
                } else {
                    Log.e("失败", String.valueOf(response.body()) + response.toString());
                    Toast.makeText(getContext(), "发布失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Post> call, Throwable t) {
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
